package com.geeksx.trainer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geeksx.trainer.common.api.Result;
import com.geeksx.trainer.entity.QuestionRecord;
import com.geeksx.trainer.service.QuestionRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 答题记录 Controller
 */
@RestController
@RequestMapping("/api/question-records")
@RequiredArgsConstructor
public class QuestionRecordController {

    private final QuestionRecordService questionRecordService;

    /**
     * 新增答题记录
     */
    @PostMapping
    public Result<QuestionRecord> create(@RequestBody QuestionRecord record) {
        questionRecordService.save(record);
        return Result.success(record);
    }

    /**
     * 获取答题记录详情
     */
    @GetMapping("/{id}")
    public Result<QuestionRecord> getById(@PathVariable Long id) {
        return Result.success(questionRecordService.getById(id));
    }

    /**
     * 获取用户的答题记录（分页）
     */
    @GetMapping("/user/{userId}")
    public Result<IPage<QuestionRecord>> getByUser(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        Page<QuestionRecord> page = new Page<>(pageNum, pageSize);
        IPage<QuestionRecord> records = questionRecordService.lambdaQuery()
                .eq(QuestionRecord::getUserId, userId)
                .orderByDesc(QuestionRecord::getCreateTime)
                .page(page);
        return Result.success(records);
    }

    /**
     * 删除答题记录
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(questionRecordService.removeById(id));
    }

    /**
     * 批量删除答题记录
     */
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return Result.success(questionRecordService.removeByIds(ids));
    }
}
